package main

import (
	"common/config"
	"common/metrics"
	"context"
	"gate/app"
	"log"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:     "connector",
	Short:   "connector 管理链接，session以及路由请求",
	Long:    `connector 管理链接，session以及路由请求`,
	Run:     func(cmd *cobra.Command, args []string) {},
	PostRun: func(cmd *cobra.Command, args []string) {},
}

var (
	configFile    string
	gameConfigDir string
	serverId      string
)

func init() {
	rootCmd.Flags().StringVar(&configFile, "config", "application.yml", "app config yml file")
	rootCmd.Flags().StringVar(&gameConfigDir, "config", "../config", "game config dir")
	rootCmd.Flags().StringVar(&serverId, "serverId", "", "app server id, required")
	_ = rootCmd.MarkFlagRequired("serverId")
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		log.Println(err)
		os.Exit(1)
	}
	config.InitConfig(configFile)
	// 读取游戏目录的配置文件
	game.InitConfig(gameConfigDir)
	go func() {
		metrics.Serv(config.Conf.MetricPort)
	}()
	// 启动grpc服务端
	err := app.Run(context.Background(), serverId)
	if err != nil {
		log.Println(err)
	}
}
